#coding=utf-8

import os
import ConfigParser
from datetime import timedelta

basedir = os.path.abspath(os.path.dirname(__file__))
conf = ConfigParser.RawConfigParser()
conf.read(os.path.join(basedir,'config.ini'))

class BaseConfig:
    SECRET_KEY = conf.get('global', 'random_key')
    DEBUG_TB_INTERCEPT_REDIRECTS = False
    SQLALCHEMY_COMMIT_ON_TEARDOWN = conf.getboolean('global', 'db_autocommit')
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    MAIL_SERVER = conf.get('smtp', 'ip')
    MAIL_PORT = conf.get('smtp', 'port')
    MAIL_USE_SSL = conf.getboolean('smtp', 'ssl')
    MAIL_USE_TLS = conf.getboolean('smtp', 'tls')
    MAIL_USERNAME = conf.get('smtp', 'user')
    MAIL_PASSWORD = conf.get('smtp', 'pwd')
    MAIL_ADDRESSEE = conf.get('smtp', 'addressee')
    MAIL_PERFIX = conf.get('smtp', 'perfix')

    @staticmethod
    def init_app(app):
        pass

class DevelopmentConfig(BaseConfig):
    DEBUG = conf.getboolean("development", "debug")
    DEBUG_TB_ENABLED = conf.getboolean("development", "debugtool")
    SQLALCHEMY_DATABASE_URI = conf.get("development", "db_url") or \
                                "sqlite:///"+os.path.join(basedir, "datas", "app-dev.sqlite")


class TestingConfig(BaseConfig):
    TESTING = conf.getboolean("testing", "test")
    DEBUG = conf.getboolean("testing", "debug")
    DEBUG_TB_ENABLED = conf.getboolean("testing", "debugtool")
    SQLALCHEMY_DATABASE_URI = conf.get("testing", "db_url") or \
                                "sqlite:///"+os.path.join(basedir, "datas", "app-test.sqlite")


class ProductionConfig(BaseConfig):
    SQLALCHEMY_DATABASE_URI = conf.get("production", "db_url") or \
                                "sqlite:///"+os.path.join(basedir, "datas", "app.sqlite")

app_config = {
            "development":DevelopmentConfig,
            "testing":TestingConfig,
            "production":ProductionConfig,
            "default":DevelopmentConfig
        }